package com.jzg.sys.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzg.sys.common.rest.BaseRest;
import com.jzg.sys.common.vo.ResultVO;
import com.jzg.sys.dao.entity.SecRole;
import com.jzg.sys.manager.SecRoleManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value = "/roleAction")
public class RoleAction extends BaseRest {
	@Autowired
	private SecRoleManager roleManager;

	@RequestMapping(value = "/list")
	public String list() {
		return "sec/role";
	}

	@RequestMapping(value = "/findEasyPage")
	@ResponseBody
	public IPage findEasyPage(Page<SecRole> easyPage, SecRole role) {
		return roleManager.findEasyPage(easyPage, role);
	}

	@RequestMapping(value = "/findById")
	@ResponseBody
	public SecRole findById(String id) {
		return roleManager.getById(id);
	}

	@RequestMapping(value = "/saveForm")
	@ResponseBody
	public ResultVO saveForm(SecRole role) {
		ResultVO vo = new ResultVO();
		try {
			if(StringUtils.startsWith(role.getRoleId(), "new")){
				role.setRoleId(role.getRoleId().replaceFirst("new", ""));
				roleManager.save(role);
			}else{
				roleManager.updateById(role);
			}
		} catch (RuntimeException be) {
			be.printStackTrace();
			vo.setResult(false);
			vo.setMsg(be.getMessage());
			return vo;
		}
		return vo;
	}

	@RequestMapping(value = "/deleteRole")
	@ResponseBody
	public ResultVO deleteRole(String id) {
		ResultVO vo = new ResultVO();
		try {
			roleManager.removeById(id);
		} catch (RuntimeException be) {
			be.printStackTrace();
			vo.setResult(false);
			vo.setMsg(be.getMessage());
			return vo;
		}
		return vo;
	}
}
